package br.ceut.especializacao.poo.autores;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import br.ceut.especializacao.poo.livros.Livro;
import br.ceut.especializacao.poo.livros.RepositorioLivros;
import br.ceut.especializacao.poo.livros.RepositorioLivrosArray;
import br.ceut.especializacao.poo.util.PersistenceMechanismException;
import br.ceut.especializacao.poo.util.PersistenceMechanismRDBMS;
import br.ceut.especializacao.poo.util.RepositorioException;

public class RepositorioAutoresBDR implements RepositorioAutores {

	private static RepositorioAutoresBDR instance;
	private PersistenceMechanismRDBMS pm;

	private RepositorioAutoresBDR() {
		try {
			pm = PersistenceMechanismRDBMS.getInstance();
			pm.connect();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public synchronized static RepositorioAutoresBDR getInstance() {
		if (instance == null) {
			instance = new RepositorioAutoresBDR();
		}
		return instance;
	}

	public void inserir(Autor autor) throws RepositorioException {
		try {
			Statement statement = (Statement) pm.getCommunicationChannel();
			String query = "INSERT INTO AUTOR VALUES (" +
					" '" + autor.getIdAutor() + "',"+
					" '" + autor.getNome() + "',"+
					" '" + autor.getNascimento() + "')";
			
			System.out.println(query);
			statement.executeUpdate(query);
			
		} catch (PersistenceMechanismException ex) {
			throw new RepositorioException(ex);
		} catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
	}

	
	public List<Autor> getAutores() throws RepositorioException {
		RepositorioAutores resposta = new RepositorioAutoresArray(100);
		try {
			Statement statement = (Statement) pm.getCommunicationChannel();
			ResultSet resultset = statement.executeQuery("SELECT * FROM AUTOR order by idAutor desc");
			while (resultset.next()) {
				Autor autor = null;
				autor = new Autor(resultset.getInt("idAutor"), resultset.getString("nome"), resultset.getString("nascimento"));
				resposta.inserir(autor);
			}
			resultset.close();
		} catch (PersistenceMechanismException e) {
			throw new RepositorioException(e);
		} catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		return resposta.getAutores();
	}

	
	
	
	public void remover(int idAutor) throws AutorNaoEncontradoException,
			RepositorioException {
		try {
			Statement statement = (Statement) pm.getCommunicationChannel();
			int i = statement.executeUpdate("DELETE FROM AUTOR WHERE idAutor='" + idAutor + "'");
			if (i == 0) {
				throw new AutorNaoEncontradoException(idAutor);
			}
		} catch (PersistenceMechanismException e) {
			throw new RepositorioException(e);
		} catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
	}

	public Autor procurar(int idAutor) throws AutorNaoEncontradoException,
			RepositorioException {
		Autor autor = null;
		try {
			Statement statement = (Statement) pm.getCommunicationChannel();
			ResultSet resultset = statement.executeQuery("SELECT * FROM AUTOR WHERE idAutor = '" + idAutor + "'");
			if (resultset.next()) {
				autor = new Autor(resultset.getInt("idAutor"), resultset.getString("nome"), resultset.getString("idAutor"));
			} else {
				throw new AutorNaoEncontradoException(idAutor);
			}
			resultset.close();
		} catch (PersistenceMechanismException ex) {
			throw new RepositorioException(ex);
		} catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		return autor;
	}

	public void atualizar(Autor autor) throws AutorNaoEncontradoException,
			RepositorioException {
		try {
			Statement statement = (Statement) pm.getCommunicationChannel();
			statement.executeUpdate("UPDATE AUTOR SET nome = '" + autor.getNome() + "', nascimento = '" + autor.getNascimento() + "' WHERE nome = '" + autor.getNome() + "'");

		} catch (PersistenceMechanismException ex) {
			throw new RepositorioException(ex);
		} catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
	}

	public boolean existe(int idAutor) throws RepositorioException {
		boolean resposta = false;
		try {
			Statement statement = (Statement) pm.getCommunicationChannel();
			ResultSet resultset = statement.executeQuery("SELECT * FROM AUTOR WHERE idAutor = " + idAutor + "");
			resposta = resultset.next();
			resultset.close();
		} catch (PersistenceMechanismException e) {
			throw new RepositorioException(e);
		} catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
			try {
				pm.releaseCommunicationChannel();
			} catch (PersistenceMechanismException ex) {
				throw new RepositorioException(ex);
			}
		}
		return resposta;
	}

	public IteratorAutores getIterator() throws RepositorioException {
		return this.getIterator();
	}

}